//
//  CHHub3.swift
//  SesameSDK
//
//  Created by eddy on 2023/12/26.
//  Copyright © 2023 CandyHouse. All rights reserved.
//

import Foundation

public protocol CHHub3: CHWifiModule2 {
    
    /// 添加SSM 設備
    /// - Parameters:
    ///   - device: device 對象
    ///   - matterProductModel: matter 模式
    ///   - result: 結果
    func insertSesame(_ device: CHDevice, matterProductModel: MatterProductModel, result: @escaping CHResult<CHEmpty>)
    
    /// IR 模式獲取
    /// - Parameter result: 0 正常模式，1 讀取模式
    func irModeGet(result: @escaping CHResult<UInt8>)

    /// 設置IR 模式
    /// - Parameters:
    ///   - mode: 0 正常模式，1 讀取模式
    ///   - result: 結果
    func irModeSet(mode: UInt8, result: @escaping CHResult<CHEmpty>)
    
    /// IR 發射
    /// - Parameters:
    ///   - id: ir id
    ///   - result: 結果
    func irCodeEmit(id: UInt8, result: @escaping CHResult<CHEmpty>)
    
    /// IR 刪除
    /// - Parameters:
    ///   - id: ir id
    ///   - result: 結果
    func irCodeDelete(id: UInt8, result: @escaping CHResult<CHEmpty>)
    
    /// IR 碼的獲取，以 publish 形式返回
    /// - Parameter result: 發送結果
    func irCodesGet(result: @escaping CHResult<CHEmpty>)
    
    /// IR 碼的編輯
    /// - Parameters:
    ///   - id: ir id
    ///   - name: ir name
    ///   - result: 結果
    func irCodeChange(id: UInt8, name: [UInt8], result: @escaping CHResult<CHEmpty>)
    
    /// 獲取matter配對碼
    /// - Parameter result: 結果
    func getMatterParingCode(result: @escaping CHResult<CHHub3MatterSettings>)
    
    /// 打開matter配對模式
    /// - Parameter result: 結果
    func openMatterPairingWindow(result: @escaping CHResult<CHEmpty>)
}

public protocol CHHub3Delegate: CHWifiModule2Delegate {
    
    /// IR 指令改變
    /// - Parameters:
    ///   - device: 設備對象
    ///   - id: ir id
    ///   - name: ir name
    func onIRCodeChanged(device: CHHub3, ir: CHHub3IRCode)
    
    /// IR 接受
    /// - Parameters:
    ///   - device: 設備對象
    ///   - id: ir id
    ///   - name: ir name
    func onIRCodeReceive(device: CHHub3, ir: CHHub3IRCode)
    
    /// IR 開始接受
    /// - Parameter device: 設備對象
    func onIRCodeReceiveStart(device: CHHub3)
    
    /// IR 完成接受
    /// - Parameter device: 設備對象
    func onIRCodeReceiveEnd(device: CHHub3)
    
    /// 接收到 mode 改變
    /// - Parameters:
    ///   - device: 設備對象
    ///   - mode: 模式 ，0 正常模式 1 錄入模式
    func onIRModeReceive(device: CHHub3, mode: UInt8)
}

public struct CHHub3IRCode {
    public let irCodeID: UInt8
    public let nameLength: UInt8
    public let irCodeName: [UInt8]?
    
    public init(irCodeID: UInt8, nameLength: UInt8, irCodeName: [UInt8]? = nil) {
        self.irCodeID = irCodeID
        self.nameLength = nameLength
        self.irCodeName = irCodeName
    }
}

public extension CHHub3IRCode {
    static func fromData(_ buf: Data) -> CHHub3IRCode {
        let codeId = buf[0]
        let nameLength = buf.count > 0 ? buf[1] : 0
        if nameLength > 0 {
            let name = buf.subdata(in: (2..<(2+Int(nameLength))))
            return CHHub3IRCode(irCodeID: codeId, nameLength: nameLength, irCodeName: name.bytes)
        } else {
            return CHHub3IRCode(irCodeID: codeId, nameLength: nameLength, irCodeName: nil)
        }
    }
    
    mutating func toData() -> Data {
        return Data(bytes: &self, count: MemoryLayout<CHHub3IRCode>.size)
    }
}

public struct CHHub3MatterSettings {
    public let qrCode: [UInt8]
    public let manualCode: [UInt8]
    
    init(qrCode: [UInt8], manualCode: [UInt8]) {
        self.qrCode = qrCode
        self.manualCode = manualCode
    }
}

public extension CHHub3MatterSettings {
    static func fromData(_ buf: Data) -> CHHub3MatterSettings? {
        guard buf.count == 35 else { return  nil }
        let qrCode = buf[safeBound: 0...21]!
        let manualCode = buf[safeBound: 22...34]!
        return CHHub3MatterSettings(qrCode: qrCode.bytes, manualCode: manualCode.bytes)
    }
}
